package com.ks.phone.view;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ks.basictools.ActivityManager;
import com.ks.basictools.BaseActivity;
import com.ks.basictools.utils.ToastUtils;
import com.ks.phone.R;
import com.ks.phone.UserManager;
import com.ks.phone.adapter.CustomerAdapter;
import com.ks.phone.constant.Constants;
import com.ks.phone.databinding.ActivityCustomerBinding;
import com.ks.phone.manager.PreferenceManager;
import com.ks.phone.responseBean.CustomerList;
import com.ks.phone.responseBean.UserResponse;
import com.ks.phone.wedget.PopupDetail;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerActivity extends BaseActivity {

    private ActivityCustomerBinding mBinding;
    private CustomerAdapter adapter;
    private int pageIndex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_customer);
        mBinding.setActivity(this);
        start();
    }

    private void start() {
        initObject();
        requestList(pageIndex);
    }

    private void initObject() {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CustomerAdapter(R.layout.item_customer);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            private PopupDetail popupDetail;

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Log.d("zhxk", "onItemClick: data = " + adapter.getData().get(position));
                if (popupDetail == null) {
                    popupDetail = new PopupDetail(CustomerActivity.this);
                }
                popupDetail.setData((CustomerList.ListDTO.DataDTO) adapter.getData().get(position));
                if (!popupDetail.isShowing()) {
                    popupDetail.showPopupWindow();
                }
            }
        });
        // 加载更多
        mBinding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    // 判断是否滚动到了底部
                    if (!recyclerView.canScrollVertically(1)) {
                        // 在这里实现上拉加载更多的功能
                        requestList(pageIndex++);
                    }
                }
            }
        });
        mBinding.recyclerView.setAdapter(adapter);
    }

    private void requestList(int index) {
        UserResponse.UserDTO user = UserManager.getInstance().getUser();
        Map<String, Object> param = new HashMap<>();
        param.put("userid", user.getId());
        param.put("token", PreferenceManager.getInstance().getToken(this));
        param.put("page", index);
        XHttp.post(Constants.URL_CUSTOMER_LIST)
                .params(param)
                .execute(new SimpleCallBack<CustomerList>() {
                    @Override
                    public void onSuccess(CustomerList list) {
                        Log.d("zhxk", "onSuccess: list = " + list);
                        loadView(list.getList().getData());
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                        Log.e("zhxk", "onError: e = " + e.getMessage());
                        ToastUtils.show(CustomerActivity.this, e.getMessage());
                    }
                });
    }

    private void loadView(List<CustomerList.ListDTO.DataDTO> data) {
        adapter.addData(data);
    }

    public void onClick_finish(View ignoreV) {
        ActivityManager.getInstance().finishActivity(this);
    }
}